package trss.webapp.view;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.feed.AbstractRssFeedView;

import trss.model.Torrent;
import trss.model.User;
import trss.webapp.util.RequestUtil;

import com.sun.syndication.feed.rss.Content;
import com.sun.syndication.feed.rss.Enclosure;
import com.sun.syndication.feed.rss.Item;

@Component("rssFeed")
public class RssView extends AbstractRssFeedView {
    /**
     * 
     */
    private static final String BITTORRENT_FILETYPE = "application/x-bittorrent";
    private static final Logger log = LoggerFactory.getLogger(AbstractRssFeedView.class);
    /**
     * 
     */
    private static final String CONTENT_TYPE = "application/rss+xml";
    //private static final String CONTENT_TYPE = "text/xml";
    
    /**
    * ${@inheritDoc}
    */
    @Override
    protected void buildFeedMetadata(Map<String, Object> model,
            com.sun.syndication.feed.rss.Channel feed,
            HttpServletRequest request) {
        User user = (User) model.get("user");
        feed.setTitle("Torrents");
        feed.setDescription("Torrents of user " + user.getUsername());
        feed.setLink(RequestUtil.getAppURL(request) + "/rss/" + user.getId());
    }

    /**
    * ${@inheritDoc}
    */
    @Override
    public String getContentType() {
        return CONTENT_TYPE;
    }

    /**
    * ${@inheritDoc}
    */
    @Override
    protected List<Item> buildFeedItems(Map<String, Object> model,
            HttpServletRequest req, HttpServletResponse resp) throws Exception {
        @SuppressWarnings("unchecked")
        List<Torrent> torrents = (List<Torrent>) model.get("torrents");
        User user = (User) model.get("user");
        List<Item> result = new ArrayList<Item>();
        for (Torrent torrent : torrents) {
            Item item = new Item();
            item.setAuthor(user.getFullName());
            item.setTitle(torrent.getMeta().getComment());
            String link = RequestUtil.getAppURL(req)+"/torrent/" + torrent.getId() + "/" + torrent.getFileName();
            item.setLink(link);
            List<Enclosure> enclosures = new ArrayList<Enclosure>();
            Enclosure enclosure = new Enclosure();
            enclosure.setLength(torrent.getFileSize());
            enclosure.setType(BITTORRENT_FILETYPE);
            enclosure.setUrl(link);
            enclosures.add(enclosure );
            item.setEnclosures(enclosures);
            Content content = new Content();
            content.setType(Content.HTML);
            String contentValue = "<p>" + torrent.getMeta().getComment() + "</p><p>" + torrent.getMeta().getCreatedBy() + "</p>";
            content.setValue(contentValue);
            item.setContent(content);
            result.add(item);
        }
        resp.setContentType(CONTENT_TYPE);
        return result ;
    }


}
